//
//  PostView.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/24.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "PostView.h"
#import "UIViewExt.h"
#define kWidth  [UIScreen mainScreen].bounds.size.width
#define kHieght [UIScreen mainScreen].bounds.size.height

#define kHeaderViewH 136
#define kHeaderCollectionH 100
#define kHeadButtonH 36
#define kHeaderViewY -100
#define kHeaderCollectionY kHeaderViewH-kHeadButtonH-kHeaderCollectionH


@implementation PostView

- (void)layoutSubviews
{
    [self _creatMainPosterCollectionView];
    [self _creatShadow];
    [self _creatHeaderView];

}

#pragma mark - 创建头视图
- (void)_creatHeaderView
{
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderViewY, kWidth, kHeaderViewH)];
    headerView.backgroundColor = [UIColor clearColor];
    [self addSubview:headerView];
    
    
    
    
    //创建CollectionViewFlowLayout
    UICollectionViewFlowLayout* headerPosterCollectionLayout = [[UICollectionViewFlowLayout alloc] init];
    headerPosterCollectionLayout.itemSize = CGSizeMake((kWidth - 60)/5, kHeaderCollectionH - 20);
    [headerPosterCollectionLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    headerPosterCollectionLayout.minimumLineSpacing = 10;

    headerPosterCollection = [[SmarllPosterView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeaderCollectionH)  collectionViewLayout:headerPosterCollectionLayout];
    headerPosterCollection.modalArray = [NSArray arrayWithArray:_modalArray];
    
    
 //   headerPosterCollection.pagingEnabled = YES;
    [headerView addSubview:headerPosterCollection];
//    [headerPosterCollection registerClass:[SmalPosterViewCelll class] forCellWithReuseIdentifier:@"posterCell"];
    
    
    //创建下拉按钮
    UIButton* posterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, kHeaderCollectionH, kWidth, kHeadButtonH)];
    [posterButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [posterButton setBackgroundImage:[UIImage imageNamed:@"indexBG_home"] forState:UIControlStateNormal];
    posterButton.tag = 100;
    [posterButton setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [headerView addSubview:posterButton];
    
    
}


#pragma mark - 创建阴影
- (void)_creatShadow
{
    _shadowView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kWidth, self.bounds.size.height)];
    [_shadowView setBackgroundColor:[UIColor colorWithWhite:0.4 alpha:0.6]];
    
    _shadowView.hidden = YES;
    
    UITapGestureRecognizer* single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction)];
    single.numberOfTapsRequired = 1;
    single.numberOfTouchesRequired = 1;
    [self addSubview:_shadowView];
    [_shadowView addGestureRecognizer:single];

}
- (void)singleTapAction
{
    UIButton* btn = (UIButton*)[self viewWithTag:100];
    headerView.top = kHeaderViewY;
    [btn setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    _shadowView.hidden = YES;
    
}


#pragma mark - 创建主CollectionView
- (void)_creatMainPosterCollectionView
{
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(kWidth*3/4, self.height*3/4);
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.minimumLineSpacing = kWidth*1/8;
    
    [layout setHeaderReferenceSize:CGSizeMake(kWidth*1/8, self.height*3/4)];
    [layout setFooterReferenceSize:CGSizeMake(kWidth*1/8, self.height*3/4)];
    

    _mainPosterCollection = [[MainPosterCollection alloc] initWithFrame:self.frame collectionViewLayout:layout];
    _mainPosterCollection.backgroundColor = [UIColor blackColor];
    _mainPosterCollection.modalArray = [NSArray arrayWithArray:_modalArray];
    
    [self addSubview:_mainPosterCollection];
}



- (void)btnAction:(UIButton*)btn
{
    [UIView beginAnimations:nil context:nil];

    NSLog(@"1");
    if(headerView.frame.origin.y == kHeaderViewY)
    {
        headerView.top = 0;
        [btn setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateNormal];
        _shadowView.hidden = NO;
    }
    else
    {
        headerView.top = kHeaderViewY;
        [btn setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
        _shadowView.hidden = YES;
    }
    
}




///

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
